//
//  FMDBSupplierCategoryManager.m
//  takeout
//
//  Created by Mac on 12-3-25.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FMDBSupplierCategoryManager.h"
#import "FMDBManager.h"
#import "FMDatabase.h"
#import "SupplierCategory.h"
@implementation FMDBSupplierCategoryManager
//查询本地所有店铺类型
-(NSArray *)getSupplierCategoryForLocal{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    FMResultSet *rs = [db executeQuery:@"SELECT * FROM supplier_category"]; 
    NSMutableArray *supplierCategories=[NSMutableArray arrayWithCapacity:8];
    while ([rs next]) {
        SupplierCategory *supplierCategory=[[SupplierCategory alloc]init];
        supplierCategory.supplierCategoryId=[rs intForColumn:@"id"];
        supplierCategory.supplierCategoryName=[rs stringForColumn:@"name"];
        [supplierCategories addObject:supplierCategory];
        [supplierCategory release];
    }
    [rs close];
    [fmdbManager closeDataBase];
    [fmdbManager release];
    return supplierCategories;
}

//插入商家类型到本地数据
-(void)insertSupplierCategory:(NSMutableDictionary *)recieveDictionary{
    FMDBManager *fmdbManager= [[FMDBManager alloc]init];
    FMDatabase *db=[fmdbManager getDataBase];
    [db executeUpdate:@"DELETE FROM supplier_category"];
    NSArray *supplierCategories=[recieveDictionary objectForKey:@"SupplierCategories"];
    for(int i =0;i<[supplierCategories count];i++){      
        [db executeUpdate:@"INSERT INTO supplier_category(id, name) VALUES(?, ?)",[NSNumber numberWithInt:[[supplierCategories objectAtIndex:i] supplierCategoryId]],[[supplierCategories objectAtIndex:i] supplierCategoryName]];
    }
    [fmdbManager closeDataBase];
    [fmdbManager release];
}
@end
